package org.misty.seckill.controller;

import org.misty.seckill.interceptor.AuthorizationInterceptor2;
import org.misty.seckill.model.User;
import org.misty.seckill.redis.LuaScript;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;

@Deprecated
@Controller
@RequestMapping("/sample")
@AuthorizationInterceptor2.IgnoreAuth
public class SampleController {
  @Autowired
  private LuaScript luaScript;
  @Autowired
  private JedisPool jedisPool;

  @GetMapping("")
  @ResponseBody
  @AuthorizationInterceptor2.RequireAuth
  public String index(@RequestAttribute(value = "user", required = false) User user) {
    var ret = new ArrayList<>();
    Jedis jedis = null;
    try {
      var s = System.currentTimeMillis();
      jedis = jedisPool.getResource();
      ret.add(System.currentTimeMillis() - s);

      s = System.currentTimeMillis();
      jedis.evalsha(luaScript.sample(), 3, "1", "2", "3", "4", "5", "6");
      ret.add(System.currentTimeMillis() - s);
    } finally {
      if (jedis != null) {
        jedis.close();
      }
    }
    System.out.println(ret);
    return "ok";
  }

  @GetMapping("/2")
  @ResponseBody
  @AuthorizationInterceptor2.IgnoreAuth
  public String index2() {
    var ret = new ArrayList<>();
    Jedis jedis = null;
    try {
      var s = System.currentTimeMillis();
      jedis = jedisPool.getResource();
      ret.add(System.currentTimeMillis() - s);

      s = System.currentTimeMillis();
      var v = jedis.exists("sample");
      ret.add(System.currentTimeMillis() - s);
    } finally {
      if (jedis != null) {
        jedis.close();
      }
    }
    System.out.println(ret);
    return "ok";
  }

  /*
  @RequestMapping("/mq/send")
  @ResponseBody
  @AuthorizationInterceptor2.IgnoreAuth
  public boolean mqSend() {
    this.mqSender.send("hello Queue");
    return true;
  }

  @RequestMapping("/mq/sendTopic")
  @ResponseBody
  @AuthorizationInterceptor2.IgnoreAuth
  public boolean mqSendTopic() {
    this.mqSender.sendTopic("hello Topic");
    return true;
  }

  @RequestMapping("/mq/sendFanout")
  @ResponseBody
  @AuthorizationInterceptor2.IgnoreAuth
  public boolean mqSendFanout() {
    this.mqSender.sendFanout("hello Fanout");
    return true;
  }

  @RequestMapping("/mq/sendHeader")
  @ResponseBody
  @AuthorizationInterceptor2.IgnoreAuth
  public boolean mqSendHeader() {
    this.mqSender.sendHeader("hello Header");
    return true;
  }
  */
}
